package com.dkd.manage.service.impl;

import com.dkd.common.constant.DkdContants;
import com.dkd.common.utils.DateUtils;
import com.dkd.common.utils.uuid.UUIDUtils;
import com.dkd.manage.domain.Channel;
import com.dkd.manage.domain.VendingMachine;
import com.dkd.manage.domain.VmType;
import com.dkd.manage.domain.vo.VmVo;
import com.dkd.manage.mapper.ChannelMapper;
import com.dkd.manage.mapper.NodeMapper;
import com.dkd.manage.mapper.VendingMachineMapper;
import com.dkd.manage.mapper.VmTypeMapper;
import com.dkd.manage.service.IVendingMachineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备管理Service业务层处理
 *
 * @author alex
 * @date 2025-03-27
 */
@Service
public class VendingMachineServiceImpl implements IVendingMachineService {
    @Autowired
    private VendingMachineMapper vendingMachineMapper;
    @Autowired
    private NodeMapper nodeMapper;
    @Autowired
    private VmTypeMapper vmTypeMapper;
    @Autowired
    private ChannelMapper channelMapper;

    /**
     * 查询设备管理
     *
     * @param id 设备管理主键
     * @return 设备管理
     */
    @Override
    public VmVo selectVendingMachineById(Long id) {
        return vendingMachineMapper.selectVendingMachineById(id);
    }

    /**
     * 查询设备管理列表
     *
     * @param vendingMachine 设备管理
     * @return 设备管理
     */
    @Override
    public List<VendingMachine> selectVendingMachineList(VendingMachine vendingMachine) {
        return vendingMachineMapper.selectVendingMachineList(vendingMachine);
    }

    /**
     * 新增设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Transactional
    public int insertVendingMachine(VendingMachine vendingMachine) {
        //填充随机八位码
        String innerCode = UUIDUtils.getUUID();
        vendingMachine.setInnerCode(innerCode);
        //填充时间
        vendingMachine.setCreateTime(DateUtils.getNowDate());
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        //补充设备容量
        Long channelMaxCapacity = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId()).getChannelMaxCapacity();
        vendingMachine.setChannelMaxCapacity(channelMaxCapacity);
        //填充默认运营状态
        vendingMachine.setVmStatus(DkdContants.VM_STATUS_NODEPLOY);
        Long nodeId = vendingMachine.getNodeId();
        Long businessType = nodeMapper.selectNodeById(nodeId).getBusinessType();
        //填充商圈类型
        vendingMachine.setBusinessType(businessType);
        //填充区域id
        vendingMachine.setRegionId(nodeMapper.selectNodeById(nodeId).getRegionId());
        //填充合作商id
        Long partnerId = nodeMapper.selectNodeById(nodeId).getPartnerId();
        vendingMachine.setPartnerId(partnerId);
        //填充详细地址
        String address = nodeMapper.selectNodeById(nodeId).getAddress();
        vendingMachine.setAddr(address);
        int result = vendingMachineMapper.insertVendingMachine(vendingMachine);
        /**
         * 2.新增货道
         */
        //创建一个存储货道对象的集合
        List<Channel> channelList = new ArrayList<>();
        VmType vmType = vmTypeMapper.selectVmTypeById(vendingMachine.getVmTypeId());
        for (int i = 1; i <= vmType.getVmRow(); i++) {//外层行遍历
            for (int j = 1; j <= vmType.getVmCol(); j++) {
                //1-1 1-2 1-3
                //封装channel对象
                Channel channel = new Channel();
                channel.setChannelCode(i + "-" + j);//货道编号
                channel.setVmId(vendingMachine.getId());
                channel.setInnerCode(vendingMachine.getInnerCode());
                channel.setMaxCapacity(vmType.getChannelMaxCapacity());//设置货道容量
                channel.setCreateTime(DateUtils.getNowDate());//创建时间
                channel.setUpdateTime(DateUtils.getNowDate());//修改时间
                //保存货道
//                channelMapper.insertChannel(channel);
                channelList.add(channel);
            }
        }
        channelMapper.batchInsertChannels(channelList);
        return result;
    }

    /**
     * 修改设备管理
     *
     * @param vendingMachine 设备管理
     * @return 结果
     */
    @Override
    public int updateVendingMachine(VendingMachine vendingMachine) {
        vendingMachine.setUpdateTime(DateUtils.getNowDate());
        //更新详细地址
        vendingMachine.setAddr(nodeMapper.selectNodeById(vendingMachine.getNodeId()).getAddress());
        //更新商圈类型
        vendingMachine.setBusinessType(nodeMapper.selectNodeById(vendingMachine.getNodeId()).getBusinessType());
        //更新合货商id
        vendingMachine.setPartnerId(nodeMapper.selectNodeById(vendingMachine.getNodeId()).getPartnerId());
        return vendingMachineMapper.updateVendingMachine(vendingMachine);
    }

    /**
     * 批量删除设备管理
     *
     * @param ids 需要删除的设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineByIds(Long[] ids) {
        return vendingMachineMapper.deleteVendingMachineByIds(ids);
    }

    /**
     * 删除设备管理信息
     *
     * @param id 设备管理主键
     * @return 结果
     */
    @Override
    public int deleteVendingMachineById(Long id) {
        return vendingMachineMapper.deleteVendingMachineById(id);
    }

    /**
     * 查询设备管理列表带点位 区域 合作商
     *
     * @param vendingMachine
     * @return
     */
    public List<VmVo> selectVmlist(VendingMachine vendingMachine) {
        List<VmVo> list = vendingMachineMapper.selectVmlist(vendingMachine);
        return list;
    }

    /**
     * 根据设备编号查询设备信息
     *
     * @param innerCode
     * @return
     */
    public VendingMachine selectVmByInnerCode(String innerCode) {
        return vendingMachineMapper.selectVmByInnerCode(innerCode);
    }


}
